// pages/goods_list/index.js
import { request } from "../request/index.js"
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
  data: {
    tabs:[
    {
       id:0,
       value:"综合",
      isActive:true
    },
    {
       id:1,
       value:"销量",
       isActive:false
    },
    {
       id:2,
       value:"价格",
       isActive:false
    }
  ],
    goodslist: [],
  },
  QueryParmams:{
    query:"", 
    cid:"",
    pagenum: 1,
    pagesize: 10
  },
  totalPages:1,
  total:0,

  onLoad: function (options) {
    this.QueryParmams.cid=options.cid;
    this.getGoodsLise();
  },
  handleTabsItemchange(e){
    const { index } = e.detail;
    let { tabs } = this.data;
    tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false);
    this.setData({
      tabs
    })
  },
  async getGoodsLise(){
    const res=await request({url:"/goods/search",data:this.QueryParmams});
    const total = res.total;
    this.totalPages=Math.ceil(total/this.QueryParmams.pagesize);
    console.log(this.totalPages);
    this.setData({
       goodslist: [...this.data.goodslist, ...res.data.message.goods]  
     })
     wx.stopPullDownRefresh();
      
  },
  onReachBottom(){
    if(this.QueryParmams.pagenum>=this.totalPages){
      wx.showToast({  title: '没有下一页数据' })  
    }else{
      this.QueryParmams.pagenum++;
      this.getGoodsLise();     
    }
  },
  onPullDownRefresh(){
    this.setData({
      goodslist:[]
    })
    this.QueryParmams.pagenum=1;
    this.getGoodsLise();
  }
})